package com.prop.utils;

import com.aspose.slides.*;
import com.aspose.words.FileFormatUtil;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.InputStream;
import java.util.Properties;

public class FileAddTextBoxInPowerPointer {

    private static Boolean checkFileSize(String filePath) {
        File file = new File(filePath);

        if (!file.exists()) {
            return false;
        }
        long fileSizeInBytes = file.length();
        long maxSizeInBytes = 15 * 1024 * 1024; // 15 MB
        if (fileSizeInBytes <= maxSizeInBytes) {
            return false;
        }
        return true;
    }


    /**
     * 在指定的 PowerPoint 文件中添加密级信息文本框。
     *
     * @param filePath PowerPoint 文件的路径。
     * @throws Exception 如果处理文件或配置过程中出现问题时抛出异常。
     */
    public static void addTextbox(String filePath, String classificationLevel, String classificationPeriod) throws Exception {

        String messageStr = FilePropertyUtils.getClassificationContext(classificationLevel,classificationPeriod);

        // 声明 Aspose 授权
        try (InputStream AsposeLicenseFile = new ClassPathResource("license.xml").getInputStream()) {
            com.aspose.slides.License license = new com.aspose.slides.License();
            license.setLicense(AsposeLicenseFile);
        }

        // 从配置文件中加载属性
        Properties properties = new Properties();
        try (InputStream input = new ClassPathResource("app.properties").getInputStream()) {
            properties.load(input);
        }

        // 判断是否需要添加文本框
        boolean addTextbox = Boolean.parseBoolean(properties.getProperty("addTextboxPowerPoint", "false"));

        if (!addTextbox) {
            return; // 如果不添加文本框，直接返回
        }

        if (checkFileSize(filePath)) {
            return; // 如果文件大于限定范围，后续会溢出，无法处理，直接返回
        }

        // 加载PPT文档
        Presentation presentation = new Presentation(filePath);
        ISlide slide = presentation.getSlides().get_Item(0); // 获取第一张幻灯片

        // 检查是否已有包含“密级”信息的文本框
        boolean containsSecret = false;
        for (IShape shape : slide.getShapes()) {
            if (shape instanceof IAutoShape) {
                IAutoShape autoShape = (IAutoShape) shape;
                if (autoShape.getTextFrame() != null && autoShape.getTextFrame().getText() != null) {
                    String text = autoShape.getTextFrame().getText();
                    if (text.contains("核心商密▲") || text.contains("普通商密▲")
                            || text.contains("秘密★") || text.contains("机密★")
                            || text.contains("非密") || text.contains("内部")) {
                        containsSecret = true;
                        break;
                    }
                }
            }
        }

        // 如果没有包含密级信息的文本框，则添加新的文本框
        if (!containsSecret) {
            float top = Float.parseFloat(properties.getProperty("textboxPowerPoint.top", "0"));
            float left = Float.parseFloat(properties.getProperty("textboxPowerPoint.left", "0"));
            float width = Float.parseFloat(properties.getProperty("textboxPowerPoint.width", "150"));
            float height = Float.parseFloat(properties.getProperty("textboxPowerPoint.height", "50"));

            // 添加文本框
            IAutoShape textBox = slide.getShapes().addAutoShape(ShapeType.Rectangle, left, top, width, height);
            String classification = messageStr;
            textBox.getTextFrame().setText(classification);
            textBox.getFillFormat().setFillType(FillType.NoFill); // 设置透明背景
            textBox.getLineFormat().getFillFormat().setFillType(FillType.NoFill); // 无边框
            // 设置文本框文字靠左对齐，靠上对齐
            textBox.getTextFrame().getParagraphs().get_Item(0).getParagraphFormat().setAlignment(TextAlignment.Left);
            textBox.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Top);


            // 从 JSON 中获取密级内容
            //String classification = FileHandlerUtils.getClassificationFromJSONStr(json);
            //String classification = "核心商密▲30年";

            // 设置文本框的文字及样式
            IPortionFormat format = textBox.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat();
            format.setFontHeight(Float.parseFloat(properties.getProperty("textboxPowerPoint.fontSize", "16")));
            format.setLatinFont(new FontData(properties.getProperty("textboxPowerPoint.fontName", "SimHei")));
            format.setEastAsianFont(new FontData(properties.getProperty("textboxPowerPoint.fontName", "SimHei"))); // 亚洲字体也需要设置成黑体


            // 保存修改后的文档，根据文件类型选择保存格式
            if (FileFormatUtil.detectFileFormat(filePath).getLoadFormat() == SaveFormat.Pptx) {
                presentation.save(filePath, SaveFormat.Pptx);
            } else {
                presentation.save(filePath, SaveFormat.Ppt);
            }
        }
    }

}
